package com.design.fitment.adapter;

/**
 * Created by 娇哥 on 2016/1/11.
 */

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.design.fitment.activity.BannerDetailActivity;
import com.design.fitment.activity.BannerOneActivity;
import com.design.fitment.app.MyApplication;
import com.design.fitment.entity.SheJiBen;

import java.util.ArrayList;
import java.util.List;

public class BannerListAdapter extends PagerAdapter {

        private List<SheJiBen.DataEntity.BannerEntity> objects = new ArrayList<>();

        private Context context;
       private int position;
        public BannerListAdapter(Context context,List<SheJiBen.DataEntity.BannerEntity> objects) {
            this.context = context;
            this.objects = objects;
        }

        @Override
        public int getCount() {
            return objects.size();
        }

    @Override
    public Object instantiateItem(ViewGroup container, final int position)
    {
        final SheJiBen.DataEntity.BannerEntity bannerList = objects.get(position);

        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        String imageUrl =bannerList.getImgUrl();


        // 使用UIL加载图片
        MyApplication.getApp()
                .getImageLoader()
                .displayImage(imageUrl, imageView,
                        MyApplication.getApp().getImageOptions());

        container.addView(imageView);
       this.position = position;
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    if (bannerList.getId()!=0){
                       // Toast.makeText(context, bannerList.getUrl()+""+bannerList.getId(), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent();
                        intent.setClass(context,BannerOneActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("bannerId", bannerList.getId());
                        context.startActivity(intent);
                    }else{
                        Intent intent = new Intent();
                        intent.setClass(context, BannerDetailActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("url", bannerList.getUrl());
                        context.startActivity(intent);
                    }




            }
        });
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object)
    {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object)
    {
        return view == object;
    }

    }


